package net.hn.hnms.biz.plan.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.ExecutionPriorityWork;
import net.hn.hnms.biz.plan.domain.PlanPriorityWork;
import net.hn.hnms.biz.plan.mapper.ExecutionPriorityWorkMapper;
import net.hn.hnms.biz.plan.mapper.PlanPriorityWorkMapper;
import net.hn.hnms.biz.plan.request.ExecutionPriorityWorkReq;
import net.hn.hnms.biz.plan.service.IExecutionPriorityWorkService;
import net.hn.hnms.biz.plan.vo.ExecutionPriorityWorkVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@DS("master")
public class ExecutionPriorityWorkServiceImpl extends BaseService<ExecutionPriorityWork, ExecutionPriorityWorkVo> implements IExecutionPriorityWorkService {

    @Resource
    private ExecutionPriorityWorkMapper baseMapper;
    @Resource
    private PlanPriorityWorkMapper planPriorityWorkMapper;


    @Override
    public int insert(ExecutionPriorityWorkReq executionPriorityWork) {
        ExecutionPriorityWork insertObj = new ExecutionPriorityWork();
        BeanUtil.copyProperties(executionPriorityWork, insertObj);
        return baseMapper.insert(insertObj);
    }


    @Override
    public void deleteBatch(List<String> idList) {
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ExecutionPriorityWorkReq executionPriorityWork) {
        if (StrUtil.isBlank(executionPriorityWork.getPriorityExecutionId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        ExecutionPriorityWork updateObj = new ExecutionPriorityWork();
        BeanUtil.copyProperties(executionPriorityWork, updateObj);
        int updateCount = baseMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ResponsePage<ExecutionPriorityWorkVo> selectPageList(ExecutionPriorityWorkReq executionPriorityWork, PageQuery pageQuery) {
        Page<ExecutionPriorityWork> page = baseMapper.selectPage(pageQuery.build(), buildQuery(executionPriorityWork).orderByDesc(ExecutionPriorityWork::getExecutionDate)
                .orderByDesc(ExecutionPriorityWork::getCreateTime));
        List<ExecutionPriorityWorkVo> list = page.getRecords().stream().map(o -> convertWithUser(o, ExecutionPriorityWorkVo.class)).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> planWorkIdList = list.stream().map(ExecutionPriorityWorkVo::getPlanWorkId).collect(Collectors.toList());
            List<PlanPriorityWork> planPriorityWorks = planPriorityWorkMapper.selectList(new LambdaQueryWrapper<PlanPriorityWork>().in(PlanPriorityWork::getPriorityPlanId, planWorkIdList));
            list.forEach(o -> {
                if (planPriorityWorks.stream().anyMatch(o1 -> o1.getPriorityPlanId().equals(o.getPlanWorkId()))) {
                    PlanPriorityWork planPriorityWork = planPriorityWorks.stream().filter(o1 -> o1.getPriorityPlanId().equals(o.getPlanWorkId())).findFirst().orElse(null);
                    if (planPriorityWork != null) {
                        o.setPlanWorkName(planPriorityWork.getPriorityPlanName());
                    }
                }
            });
        }
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public ExecutionPriorityWorkVo selectDetailById(String id) {
        ExecutionPriorityWork executionInfo = baseMapper.selectById(id);
        if (executionInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(executionInfo, ExecutionPriorityWorkVo.class);
    }


    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<ExecutionFootageOutput> 查询条件
     */
    private LambdaQueryWrapper<ExecutionPriorityWork> buildQuery(ExecutionPriorityWorkReq req) {
        LambdaQueryWrapper<ExecutionPriorityWork> lqw = new LambdaQueryWrapper<ExecutionPriorityWork>()
                .ge(req.getBeginDateTime() != null, ExecutionPriorityWork::getExecutionDate, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ExecutionPriorityWork::getExecutionDate, req.getEndDateTime())
                .eq(StrUtil.isNotBlank(req.getExecutorUserName()), ExecutionPriorityWork::getExecutorUserName, req.getExecutorUserName())
                .and(o -> o.eq(req.getMineCode() != null, ExecutionPriorityWork::getMineCode, req.getMineCode())
                        .or()
                        .eq(req.getCompanyCode() != null, ExecutionPriorityWork::getCompanyCode, req.getCompanyCode())
                );
        return lqw;
    }


}
